<?
header("Content-type: image/jpeg");

function thumb($picture,$max,$sz,$dir){
	//$max=150;    # maximum size of 1 side of the picture.
	
	$picture=str_replace("-qm-","?",$picture);
	
	/*
	here you can insert any specific "if-else",
	or "switch" type of detector of what type of picture this is.
	in this example i'll use standard JPG
	*/
	if (strpos($picture,".jpg")!==false) $src_img=imagecreatefromjpeg($dir.$picture);
	if (strpos($picture,".png")!==false) $src_img=imagecreatefrompng($dir.$picture);
	if (strpos($picture,".gif")!==false) $src_img=imagecreatefromgif($dir.$picture);
	
	$oh = imagesy($src_img);  # original height
	$ow = imagesx($src_img);  # original width
	
	$new_h = $oh;
	$new_w = $ow;
	
	if ($sz=="w"){
		$r=$oh/$ow;
		$new_w=$max;
		$new_h=$max*$r;
	}elseif ($sz=="h"){
		$r=$ow/$oh;
		$new_w=$max*$r;
		$new_h=$max;
	}elseif($oh > $max || $ow > $max){
		   $r = $oh/$ow;
		   $new_h = ($oh > $ow) ? $max : $max*$r;
		   $new_w = $new_h/$r;
	}
	// note TrueColor does 256 and not.. 8
	$dst_img = imagecreatetruecolor($new_w,$new_h);
	
	imagecopyresized($dst_img, $src_img, 0,0,0,0, $new_w, $new_h, imagesx($src_img), imagesy($src_img));
	
	echo imagejpeg($dst_img);
	imagedestroy($dst_img);
}
if (!isset($_REQUEST["sz"])) $_REQUEST["sz"]="";
if (!isset($_REQUEST["dir"])) $_REQUEST["dir"]="../";
thumb($_REQUEST["image"],$_REQUEST["max"],$_REQUEST["sz"],$_REQUEST["dir"]);
?>